<?php

/*
    picopress - A lightweighed online publishing system
    Copyright (C) 2010  Martin Burkert <martin@burkert.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
*/

class CoreTemplate implements Template {

    public $config;
    public $name = '';
    public $title;
    public $node_title;    
    public $breadcrumbs_html;    
    public $content_html;
    public $menu_html;
    public $header_html;
    public $menu_structure;
    public $vars = Array();
    public $menu_prediscover_depth = 0;


    public function set_config (&$config){
        $this->config = $config;
    }

    // this is an alias function for the core function get_base_url for easier syntax in templates
    public function get_base_url () {
        return get_base_url();
    }
    
    public function get_template_dir () {
        
        return 'templates/'.$this->name;
    }
    
    public function get_header_html() {
        return $this->header_html;
    }

    public function get_menu_html() {
        return $this->menu_html;
    }
    
    public function get_content_html() {
        return $this->content_html;
    }    
    
    public function get_breadcrumbs_html() {
        return $this->breadcrumbs_html;
    }    
    
    public function get_title() {
        return $this->title;
    }    

    public function assign_title($title) {
        $this->title = $title;
        $this->vars['title'] = $title;
    }   

    public function get_node_title() {
        return $this->node_title;
    }  

    public function assign_node_title($node_title) {
        $this->node_title = $node_title;
        $this->vars['node_title'] = $node_title;
    }   
    
    public function assign_var ($var, $value){
        $this->vars[$var] = $value;
    }


    public function get_var ($var){
        return $this->vars[$var];
    }
        
    public function assign_content ($content_html){
    
        $this->content_html = $content_html;
    }

    public function add_to_header ($element){
    
        $this->header_html .= $element;
    
    }
    
    public function get_menu_prediscover_depth() {
    
        return $this->menu_prediscover_depth;
    }
        
    public function assign_menu ($menu_structure){
    
        $this->menu_structure = $menu_structure;    
        $this->menu_html = $this->construct_menu($menu_structure, 0);
        $this->breadcrumbs_html = $this->construct_breadcrumbs($menu_structure);

    }

    public function construct_menu ($menu_structure, $level) {
    
        $menu_html = '';
        $ul_classes = '';
        
        if($level == 0)
            $ul_classes = 'menu topmenu level'.$level;
        else
            $ul_classes = 'menu submenu level'.$level;


        $menu_html .= '<ul class="'.$ul_classes.'">';
        $deeper_level_keys = array();

        foreach ($menu_structure as $key => $entry) {

            if(isset($entry['struct']) && is_array($entry['struct'])){
                array_push($deeper_level_keys, $key);
            }
        
            if ($entry['selected'] == 1)
                $menu_html .= '<li class="selected">';
            else
                $menu_html .= '<li>';                

            $menu_html .= "<a href=\"".create_link($entry['node'])."\">".
                          $entry['title']."</a></li>";

        }

        $menu_html .= '</ul>';

        foreach ($deeper_level_keys as $dl_key) {
            $menu_html .= $this->construct_menu($menu_structure[$dl_key]['struct'], $level+1);
        }
    
        return $menu_html;

    }

    public function construct_breadcrumbs ($menu_structure) {
    
        $breadcrumbs_html = '<ul class="breadcrumbs">';
        $examine_struct = true;
        
        if(isset($menu_structure[0]) && $menu_structure[0]['selected'] == 1) {
            $breadcrumbs_html .= '<li>'.$this->config['root_node_title'].'</li>';
            $examine_struct = false;
        }else {
            $breadcrumbs_html .= '<li><a href="'.create_link('').'">'.
                                 $this->config['root_node_title'].
                                 '</a><span class="bread_gt">&gt;</span></li>';
        }

    
        $crumbs = array();
    
        if ($examine_struct) {
            $sub_struct = $menu_structure;

            while ($element = array_shift($sub_struct)) {

                if($element['selected'] == 1) {
    
                    if(is_array($element['struct'])) {
                        $sub_struct = $element['struct'];
                    }else {
                        $sub_struct = array();
                    }

                    array_push($crumbs, array('title' => $element['title'], 
                                              'link' => create_link($element['node'])));
                
                }

            
            }
        }
        
        $crumb_count = count($crumbs);
        for($i=0; $i<$crumb_count; $i++) {
        
            $crumb = $crumbs[$i];
        
            if($i < $crumb_count - 1){
                $breadcrumbs_html .= '<li><a href="'.$crumb ['link'].'">'.$crumb ['title'].
                                     '</a><span class="bread_gt">&gt;</span></li>';
            }else{
                $breadcrumbs_html .= '<li>'.$crumb ['title'].'</li>';
            }
        }
        
        
        
        $breadcrumbs_html .= '</ul>';
        return $breadcrumbs_html;
    }

    public function display (){

        require($this->get_template_dir().'/html/template.php');
 
    }
    
}    

?>